/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tkgarment.system.action;

import com.tkgarment.system.dao.DepartmentDAO;
import com.tkgarment.system.domain.Department;
import com.tkgarment.system.factory.DAOFactory;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author zraj
 */
public class DepartmentJson extends ActionSupportBase implements SessionAware {

    
    Map<String , Object> session;
    private static final long serialVersionUID = 1L;
    private static final Log log = LogFactory.getLog(Department.class);
    
    List<Department> gridModel;
    List<Department> myList;

    public String execute() throws Exception {
        log.debug("Page " + getPage() + " Rows " + getRows() + " Sorting Order " + getSord() + " Index Row :" + getSidx());
        log.debug("Search :" + getSearchField() + " " + getSearchOper() + " " + getSearchString());

        Object list = session.get("deptlist");
        if (list != null) {
            
            myList = (List<Department>) list;
            System.out.println(myList.size());
           
        } else {
            log.debug("Get All List");
            DepartmentDAO dao = DAOFactory.createDepartmentDao();
            myList = dao.getAll();
            System.out.println(myList.size());
        }

//        if (sord != null && sord.equalsIgnoreCase("asc")) {
//            Collections.sort(myEmps);
//        }
//        if (sord != null && sord.equalsIgnoreCase("desc")) {
//            Collections.sort(myEmps);
//            Collections.reverse(myEmps);
//        }
        // Count all record (select count(*) from your_custumers)
        setRecords(myList.size());

        if (getTotalrows() != null) {
            setRecords(getTotalrows());
        }

        // Calucalate until rows ware selected
        int to = (getRows() * getPage());

        // Calculate the first row to read
        int from = to - getRows();

        // Set to = max rows
        if (to > getRecords()) {
            to = getRecords();
        }

        if (isLoadonce()) {
            if (getTotalrows() != null && getTotalrows() > 0) {
                setGridModel(myList.subList(0, getTotalrows()));
            } else {
                // All Custumer
                setGridModel(myList);
            }
        }

        // Calculate total Pages
        setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));

        // only for showcase functionality, don't do this in production
//        session.put("deptlist", myList);

        return SUCCESS;
    } 
    
    public String getJSON() throws Exception {
        return execute();
    }
    
    
    // getter and setter
    
    
    
    public List<Department> getGridModel() {
        return gridModel;
    }

    public void setGridModel(List<Department> gridModel) {
        this.gridModel = gridModel;
    }

    public List<Department> getMyList() {
        return myList;
    }

    public void setMyList(List<Department> myList) {
        this.myList = myList;
    }
    
  
    @Override
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }
    
}
